﻿using System.Collections.Generic;
using Newtonsoft.Json;
using T.FAS.Runtime.DataAccess.Common;

namespace T.FAS.Runtime.DataAccess.Engine.ValidateChecker.Entity
{
    /// <summary>
    /// 校验项
    /// </summary>
    public class ValidateCheckItem : IValidateCheckItem
    {
        [JsonIgnore]
        public IValidateCheckElement CheckElement { get; set; }
        private object _inputValue { get; set; }
        /// <summary>
        /// 输入原始值
        /// </summary>
        public object InputValue
        {
            get
            {
                return _inputValue;
            }
            set
            {
                _inputValue = AbstractDataTypeTool.ProcessSpecialObject(value);
            }
        }
        /// <summary>
        /// 类型转换后的值
        /// </summary>
        public object ConvertValue { get; set; }
        public ItemCheckResult Result { get; set; } = new ItemCheckResult();
        protected IValidaterFactory Factory
        {
            get
            {
                return ValidateFacadeFactory.GetFactory(CheckElement.DataType);
            }
        }

        public Dictionary<string, object> ExtInfo { get; set; }

        public ValidateCheckItem()
        {
            //CheckElement = SourceCNOElement;
            //factory = ValidateFacadeFactory.GetFactory(SourceCNOElement.DataType);
            Result.ValidateCheckItem = this;    //获取CheckItem引用
        }

        public ValidateCheckItem CheckInput()
        {
            //this.Result.ValidateCheckItem = this;    //获取CheckItem引用
            //var factory = ValidateFacadeFactory.GetFactory(SourceCNOElement.DataType);
            //var v1 = factory.GetNullValidater();
            //var v2 = factory.GetDataTypeValidater();
            //var v3 = factory.GetLengthValidater();
            //var v4 = factory.GetPrecisionValidater();
            //v1.Validate(this);
            //v2.Validate(this);
            //v3.Validate(this);
            //v4.Validate(this);
            CheckNull().CheckDataType().CheckLength().CheckPrecision().CheckValidateRule();
            return this;
        }

        public ValidateCheckItem CheckNull()
        {
            var v1 = Factory.GetNullValidater();
            v1.Validate(this);
            return this;
        }
        public ValidateCheckItem CheckDataType()
        {
            var v1 = Factory.GetDataTypeValidater();
            v1.Validate(this);
            return this;
        }
        public ValidateCheckItem CheckLength()
        {
            var v1 = Factory.GetLengthValidater();
            v1.Validate(this);
            return this;
        }
        public ValidateCheckItem CheckPrecision()
        {
            var v1 = Factory.GetPrecisionValidater();
            v1.Validate(this);
            return this;
        }
        public ValidateCheckItem CheckValidateRule()
        {
            var v1 = Factory.GetValidateRuleValidater();
            v1.Validate(this);
            return this;
        }

        public ItemCheckResult Check()
        {
            return CheckInput().Result;
        }
    }
}
